﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.Common;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class AdminUserService : IAdminUserService
    {
        public long AddAdminUser(string name, string phoneNum, string passWord, string email, long? cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cm = new CommonService<AdminUserEntity>(ct);
                if (cm.GetAll().Any(s=>s.PhoneNum==phoneNum))
                {
                    throw new ArgumentException("手机号已经存在");
                }
                AdminUserEntity ad = new AdminUserEntity() { Name = name, PhoneNum = phoneNum, Email = email, CityId = cityId };
                string yan = CommonHelper.GenerateCaptchaCode(5);//盐
                ad.PasswordYan = yan;
                ad.PasswordHash = (yan + passWord).CalcMD5();
                ct.AdminUsers.Add(ad);
                ct.SaveChanges();
                return ad.Id;
            }
        }

        public bool CheckLogin(string user, string pass)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> adc = new CommonService<AdminUserEntity>(ct);
                AdminUserEntity a = adc.GetAll().SingleOrDefault(s=>s.PhoneNum==user);
                if (a==null)
                {
                    return false;
                }
               string check =  (a.PasswordYan+pass).CalcMD5();
                return a.PasswordHash == check;
            }
        }
        private AdminUserDTO ToDTO(AdminUserEntity e)
        {
            AdminUserDTO d = new AdminUserDTO();
            d.CityID = e.CityId;
            if (e.CityId!=null)
            {
                d.CityName = e.City.Name;
            }
            d.CreateTime = e.CreateDatatime;
            d.Email = e.Email;
            d.Id = e.Id;
            d.LastLoginErrorDateTime = e.LastLoginErrorTime;
            d.LoginErrorTimes = e.LoginErrorTotal;
            d.Name = e.Name;
            d.PhoneNum = e.PhoneNum;
            return d;
        }

        public AdminUserDTO[] GetALLByCityId(long? cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cad = new CommonService<AdminUserEntity>(ct);
                var all = cad.GetAll().Include(s => s.City).AsNoTracking().Where(a=>a.CityId==cityId);
                List<AdminUserDTO> dtos = new List<AdminUserDTO>();
                foreach (var item in all)
                {
                    dtos.Add(ToDTO(item));
                }
                return dtos.ToArray();
            }
        }

        public AdminUserDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> a = new CommonService<AdminUserEntity>(ct);
                if (!a.GetAll().Any(b=>b.Id==id))
                {
                    throw new Exception("没有这个用户");
                }
                return ToDTO(a.GetById(id));
            }
        }

        public bool HasPermission(long id, string permissionName)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cs = new CommonService<AdminUserEntity>(ct);
                return cs.GetById(id).Roles.SelectMany(r => r.Permissions).Any(p => p.Name == permissionName);
            }
        }

        public void MarkDeleted(long id)
        {
            using (ZSZDBcontext ct =new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> ad = new CommonService<AdminUserEntity>(ct);
                ad.MarkDeleted(id);
            }
        }

        public void RecordLoginError(long id)
        {
            throw new NotImplementedException();
        }

        public void ResetLoginError(long id)
        {
            throw new NotImplementedException();
        }

        public void UpdateAdminUser(long adminuserId,string name, string pass, string email, long? cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> ad = new CommonService<AdminUserEntity>(ct);
                var en = ad.GetAll().SingleOrDefault(s=>s.Id== adminuserId);
                if (en==null)
                {
                    throw new ArgumentException("没有这个用户");
                }
                en.Name = name;
                en.PasswordHash = ( en.PasswordYan+pass).CalcMD5();
                en.Email = email;
                en.CityId = cityId;
                ct.SaveChanges();
            }
        }

        public AdminUserDTO[] GetAll()
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cs = new CommonService<AdminUserEntity>(ct);
                return cs.GetAll().Include(a => a.City).AsNoTracking().ToList().Select(a => ToDTO(a)).ToArray(); ;
            }
        }

        public AdminUserDTO GetByPhone(string phone)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cm = new CommonService<AdminUserEntity>(ct);
                if (!cm.GetAll().Any(s => s.PhoneNum == phone))
                {
                    throw new ArgumentException("手机号不存在");
                }
                return ToDTO(cm.GetAll().SingleOrDefault(a=>a.PhoneNum==phone));
            }
        }
    }
}
